/**
 *   FuzzyEngine - an Simple flex framework 
 *   Copyright (C) 2010 Constantin Aldea
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package main.com.sibilla.gadgets.views {
	import mx.containers.TitleWindow;
	import mx.containers.VBox;
	import mx.controls.ProgressBar;
	import mx.controls.Text;
	import mx.core.Application;
	import mx.managers.PopUpManager;

	import flash.display.DisplayObject;
	import flash.events.IEventDispatcher;

	public class LoadingPopup extends TitleWindow implements IEventDispatcher {
		public static const POP_DURATION : int = 20000;
		private static const WARNING : String = "WARNING";

		/* private var _timer:Timer; */

		public function LoadingPopup() {
			super();
			this.title = WARNING; 			this.title = WARNING; 
			this.setStyle("textAlign", "left");
			this.setStyle("backgroundColor", "#E5E4E0");	
			this.setStyle("borderStyle", "solid");
			//this.setStyle("backgroundAlpha", 0.2);
			this.horizontalScrollPolicy = "off";
			this.verticalScrollPolicy = "off";		
			this.width = 330;
			showCloseButton = false;
			loader = new ProgressBar;
			loader.indeterminate = true;
			loader.percentWidth = 100;
			loader.setStyle("fontSize", 9);
 			//this.addEventListener(CloseEvent.CLOSE, closeInstance);
 	/* 		this._timer = new Timer();
 			this._timer.delay = WarningPopup.POP_DURATION;
 			this._timer.start(); */
		}

		
		//----------------------------------
		//  SETTER / GETTER FOR PUBLIC PROPS
		//----------------------------------

		private var _message : String = 'in inizializzazione...';
		private var _vBox : VBox;
		private var loader : ProgressBar = new ProgressBar;
		private var text : Text;

		
		public function set message(value : String) : void {
			loader.label = value;
   	 		
			this.invalidateProperties();
		}

		
		//----------------------------------
		//  PRIVATE METHODS
		//----------------------------------   	 	
		public function removeLoading() : void {
			/* var sequence:Sequence = new Sequence(this); */
			//var disp:LoadingPopup =  e.currentTarget as LoadingPopup;
			PopUpManager.removePopUp(this);
			message = '';
			/* sequence.addEventListener(EffectEvent.EFFECT_END, function():void{
				PopUpManager.removePopUp(disp);
			});
			sequence.suspendBackgroundProcessing = true;
			var pause:Pause = new Pause();
			pause.duration = 400;
			sequence.addChild(pause);
			var resize:Resize = new Resize();
			resize.heightTo = 1;
			resize.duration = 300;
			resize.easingFunction = Circular.easeInOut;
			resize.suspendBackgroundProcessing = true;
			sequence.addChild(resize);
			var pause2:Pause = new Pause();
			pause2.duration = 300;
			pause2.suspendBackgroundProcessing = true;
			sequence.addChild(pause2);
			var resize2:Resize = new Resize();
			resize2.widthTo = 1;
			resize2.duration = 300;
			resize2.suspendBackgroundProcessing = true;
			resize2.easingFunction = Circular.easeOut;
			sequence.addChild(resize2);
			sequence.play(); */
		}

		
		public function showLoading(input : String = "") : void {
			PopUpManager.addPopUp(this, Application.application as DisplayObject, true);
			PopUpManager.centerPopUp(this);
			if(input) message = input;
		}

		override protected function commitProperties() : void {
			super.commitProperties();
		}

		override protected function createChildren() : void {
			super.createChildren();
			text = new Text();
			text.setStyle("fontSize", 7);
			text.text = _message;
			text.percentWidth = 100;
			text.percentHeight = 100;	
			text.width = this.width - 20;
			
			_vBox = new VBox();
	 		
			this.addChild(loader);
			this.addChild(_vBox);
			this.x = stage.width - this.width - 30;
			this.y = 20;
		}

		//----------------------------------
		//  OVERRIDEN METHODS
		//----------------------------------

		override protected function measure() : void {
			super.measure();
		}
	}
}